
const { app, BrowserWindow, Menu, Tray } = require('electron')
const path = require('path')

app.whenReady().then(()=>{
  createWindow()
})

// 创建窗口
let win = null
let tray = null
function createWindow() {
  const width = 300
  const height = 430
  win = new BrowserWindow({
    width: width,
    height: height,
    frame: false,
    show:false,
    resizable:false,
    movable:false
  })
  win.loadFile(path.join(__dirname,'../renderer/index.html'))

  // 在所有工作空间都显示-兼容mac多桌面滑动切换
  win.setVisibleOnAllWorkspaces(true)

  win.on('blur', () => {
    win.hide()
  })
  createTray(width,height)
}

// 创建托盘
function createTray(width,height) {
  tray = new Tray(path.join(__dirname, '../../../doc/logoTemplate.png'))
  // const contextMenu = Menu.buildFromTemplate([])
  // // 设置托盘菜单
  // tray.setContextMenu(contextMenu)
  // 设置悬浮提示
  tray.setToolTip('点击弹出计算器')
  tray.on('click',()=> {
    const trayBounds = tray.getBounds()
    console.log(trayBounds,'trayBounds')
    let posX = trayBounds.x + trayBounds.width / 2 - width / 2
    let posY = trayBounds.y > height ? trayBounds.y - trayBounds.height-height : trayBounds.y + trayBounds.height
    win.setPosition(posX, posY)
    if (win.isVisible()) {
      win.hide()
    } else {
      win.show()
    }
  })
}